#!/usr/bin/env python
# -*- coding: iso-8859-2 -*- 
from __future__ import division
from math import floor, ceil
        
def rgb2hsv(rgb):
    """Konwertuje RGB na HSV, zwraca tuple"""
    R, G, B = rgb
    __max = max( rgb )
    __min = min( rgb )
    __delta = __max - __min

    V = __max
    
    if __max != 0:
        S = __delta / float(__max )

    else:
        S = 0
        H = 0

    if S != 0:
        if R == __max:
            H = ( G - B ) / __delta
        elif G == __max:
            H = 2 + ( B - R ) / __delta
        else:
            H = 4 + ( R - G ) / __delta
    else:
        H = 0

    H = H * 60
    if H < 0:
        H = H + 360
    return H, S, V

def hsv2rgb(hsv):
    """Konwertuje HSV na RGB, zwraca tuple"""    
    h, s, v = hsv
    if s == 0:
        return v, v, v
    h = h/60
    i =  floor( h )
    f = h - i
    p = v * ( 1 - s )
    q = v * ( 1 - s * f )
    t = v * ( 1 - s * ( 1 - f ) )
    
    if i == 0:
        r = v
        g = t
        b = p
    elif i == 1:
        r = q
        g = v
        b = p
    elif i == 2:
        r = p
        g = v
        b = t
    elif i == 3:
        r = p
        g = q
        b = v
    elif i == 4:
        r = t
        g = p
        b = v
    else:
        r = v
        g = p
        b = q
    return  int(r), int(g), int(b)

def getHumanColor(c, hsv):
	H, S, V = hsv
	if H<=c.Hh and H>=c.Hl and S<=c.Sh and S>=c.Sl and V<=c.Vh and V>=c.Vl :
		return 1
	else:
		return 0 
